package com.jams.goodox.activity.mine.buy;

import android.content.Intent;
import android.graphics.Rect;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.activity.mine.message.MineMessageBean;
import com.jams.goodox.activity.qa.ClassLearningActivity;
import com.jams.goodox.net.BaseEntity;
import com.jams.goodox.net.ProtocolManagers;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.MineService;
import com.jams.goodox.utils.StatusBarCompat;
import com.jams.goodox.utils.StyleUtil;
import com.jams.library.commonAdapter.RecycleCommonAdapter;
import com.jams.library.commonAdapter.RecycleCommonViewHolder;
import com.jams.library.listener.OnRecycleItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import retrofit2.Call;

/**
 * Created by Administrator on 2018/5/12.
 */

public class MineBuyActivity extends BaseActivity{
    @Bind(R.id.recycle_view)
    RecyclerView recycleView;

    private List<MyBuyBean> myBuyBeanList;
    @Override
    protected int getLayoutId() {
        return R.layout.mine_buy_activity;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this,"已购买");
        StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.main_theme));//沉侵式头部
        recycleView.setLayoutManager(new LinearLayoutManager(this));
//        final int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.active_list_hint_height);
//        recycleView.addItemDecoration(new RecyclerView.ItemDecoration(){
//            @Override
//            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
//                outRect.top = spacingInPixels;
//            }
//        });
        myBuyBeanList=new ArrayList<>();
        recycleView.setAdapter(new RecycleCommonAdapter(this,myBuyBeanList,R.layout.classroom_item) {

            @Override
            public void convert(RecycleCommonViewHolder helper, final Object item, int position) {
                MyBuyBean myBuyBean = (MyBuyBean) item;
                helper.setText(R.id.title, myBuyBean.getTitle());
                helper.setText(R.id.content, myBuyBean.getDescr());
                helper.setText(R.id.nickName, myBuyBean.getNickName());
                helper.setSimpleDraweeView(R.id.imageView,myBuyBean.getImgPath());

                //Glide.with(context).load(classRoomBean.getImgPath()).into(holder.imageView);
                helper.setOnItemClickListener(new OnRecycleItemClickListener() {
                    @Override
                    public void itemClick() {
                        MyBuyBean myBuyBean = (MyBuyBean) item;
                        Intent intent = new Intent(MineBuyActivity.this,ClassLearningActivity.class);
                        intent.putExtra("courseId",myBuyBean.getCourseId());
                        intent.putExtra("imagePath",myBuyBean.getImgPath());
                        intent.putExtra("nickName",myBuyBean.getNickName());
                        startActivity(intent);
                    }
                });
            }
        });
        getData();
    }

    private void getData(){
        enqueue(createCallService(MineService.class).queueBuy(ProtocolManagers.getDefaultMap()), new RetrofitCallbackListener<BaseEntity>() {
            @Override
            public void onSuccess(Call<BaseEntity> call, BaseEntity result) {
                if (TextUtils.isEmpty(result.getResponseData())) return;
                try{
                    JsonArray jsonArray = new JsonParser().parse(result.getResponseData()).getAsJsonArray();
                    myBuyBeanList.clear();
                    for(JsonElement jsonElement:jsonArray){
                        MyBuyBean message = new Gson().fromJson(jsonElement, MyBuyBean.class);
                        myBuyBeanList.add(message);
                    }
                    recycleView.getAdapter().notifyDataSetChanged();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String msg) {

            }
        });
    }
}
